<?php
	//create by L.
	//支付宝充值回调接口-alipay_result.php
	//与支付宝对接，通知我方支付结果
	//http://42.121.57.193:7788/platform/alipay_result.php

	include_once "init.php";
	/*
	//echo dirname(__FILE__);
	//return ;
	//$pubKey = file_get_contents('C:/APMServ5.2.6/www/htdocs/sqh/core/key/'.'alipay_public_key.pem');
	$pubKey = file_get_contents('core/key/alipay_public_key.pem');
	echo $pubKey;
	return ;
	$d = 'notify_data=<notify><partner>2088702043538774</partner><discount>0.00</discount><payment_type>1</payment_type><subject>骨头1</subject><trade_no>2011102716746901</trade_no><buyer_email>4157874@qq.com</buyer_email><gmt_create>2011-10-27 12:10:51</gmt_create><quantity>1</quantity><out_trade_no>1027040323-9215</out_trade_no><seller_id>2088702043538774</seller_id><trade_status>TRADE_FINISHED</trade_status><is_total_fee_adjust>N</is_total_fee_adjust><total_fee>0.01</total_fee><gmt_payment>2011-10-27 12:10:52</gmt_payment><seller_email>17648787@qq.com</seller_email><gmt_close>2011-10-27 12:10:52</gmt_close><price>0.01</price><buyer_id>2088002456173013</buyer_id><use_coupon>N</use_coupon></notify>';

	$s ='HQq7oZ+gvdSDwV4bQh8QO0TdbwG2gEwjtY/fJbZWiuMEBwSG1VBdsQsocfuNALrEEGCFEIDM+bBjFN/idR+zV2CoGuRuofoZP4mEJ28+4p16iapilTr51boBTd2lQmMd8RQjlkNAz6QF6J5soCDH3WjI4pF75U0OqupacF9q7JA=';
	$isVerify = verify($d, $s);

	echo '返回:'.$isVerify;
	return ;
	*/
    //不需要解密，是明文的格式
	$notify_data 	= "notify_data=" . $_POST["notify_data"];
    //获取sign签名
    $sign 			= $_POST["sign"];

	log_file(date('Y-m-d H:i:s').':'.$notify_data,'/tmp/alipaylog.txt');

	log_file('验签 sign:'.$sign,'/tmp/alipaylog.txt');
	//验证签名
    $isVerify = verify($notify_data, $sign);
    log_file('验签返回:'.$isVerify,'/tmp/alipaylog.txt');
	//如果验签没有通过
    if(!$isVerify)
    {
    	die("fail");
    }
	else
	{
		echo "true";
	}


	if (empty($_POST["notify_data"]))
	{
		die('POST数据不能为空');
	}
    //获取交易状态
    $trade_status 	= getDataForXML($_POST["notify_data"], '/notify/trade_status');
    $partner 		= getDataForXML($_POST["notify_data"], '/notify/partner');
    $discount 		= getDataForXML($_POST["notify_data"], '/notify/discount');
    $payment_type 	= getDataForXML($_POST["notify_data"], '/notify/payment_type');
    $subject		= getDataForXML($_POST["notify_data"], '/notify/subject');
    $trade_no 		= getDataForXML($_POST["notify_data"], '/notify/trade_no');
    $buyer_email 	= getDataForXML($_POST["notify_data"], '/notify/tbuyer_email');
    $gmt_create 	= getDataForXML($_POST["notify_data"], '/notify/gmt_create');
    $quantity 		= getDataForXML($_POST["notify_data"], '/notify/quantity');
	$out_trade_no 	= getDataForXML($_POST["notify_data"], '/notify/out_trade_no');
	$seller_id 		= getDataForXML($_POST["notify_data"], '/notify/seller_id');
  	$is_total_fee_adjust 	= getDataForXML($_POST["notify_data"], '/notify/is_total_fee_adjust');
	$total_fee 		= getDataForXML($_POST["notify_data"], '/notify/total_fee');
  	$gmt_payment 	= getDataForXML($_POST["notify_data"], '/notify/gmt_payment');
  	$gmt_close 		= getDataForXML($_POST["notify_data"], '/notify/gmt_close');
  	$price 			= getDataForXML($_POST["notify_data"], '/notify/price');
  	$buyer_id 		= getDataForXML($_POST["notify_data"], '/notify/buyer_id');
  	$use_coupon 	= getDataForXML($_POST["notify_data"], '/notify/use_coupon');

	log_file($trade_status.':'.$out_trade_no,'/tmp/alipaylog.txt');

    //判断交易是否完成-TRADE_FINISHED
    if($trade_status == "TRADE_FINISHED")
    {
    	echo "success";

    	//在此处添加您的业务逻辑，作为收到支付宝交易完成的依据

    	//1st--reslut,flag,create_time 更新alipay表字段
    	$con    = db_connect(DB_NAME);
    	$result = mysql_query("SELECT * FROM alipay where uuid='$out_trade_no'") or die("-1|查询alipay表失败");
		$row 	= mysql_fetch_row($result);

		if (empty($row))
		{
			mysql_close($con);
			die("-1|订单号不存在");
			log_file('error:'.$trade_no.'订单号不存在\r\n','/tmp/alipaylog.txt');
		}

		//字段参数配置
    	$flag 			= '1';
    	$phone			= $row['1'];
    	$money			= $row['2'];
		$result = mysql_query("UPDATE alipay SET result = '$notify_data',flag='$flag' where uuid='$out_trade_no'") or die("-1|更新alipay失败");
		//log_file('更新alipay表的返回值:'.$result,'/tmp/alipaylog.txt');
		mysql_close($con);
		if ( $result === false )
		{
			die("-1|error:update");
			log_file('更新alipay表失败\r\n','/tmp/alipaylog.txt');
		}
		else
		{
			//2st--调用修改余额接口，修改余额
			$result  = setbalance($phone,$money);

			log_file('修改余额返回值：'.$result,'/tmp/alipaylog.txt');

			if('0'==$result[0] && '|'==$result[1])
			{
				echo '0|';
			}
			else
			{
				die("-1|error:setbalance ");
			}
		}



    }
    else
    {
    	echo "fail";
    	log_file('失败-返回非TRADE_FINISHED','/tmp/alipaylog.txt');
    }



?>